//
//  BeforOrAfterDayCalculate.m
//  安好
//
//  Created by qinlongqing on 14/11/19.
//  Copyright (c) 2014年 lianchuangbrother. All rights reserved.
//

#import "BeforOrAfterDayCalculate.h"

@implementation BeforOrAfterDayCalculate

/**
 *  得到时区的偏差
 *
 *  @return 偏差量
 */
+ (NSInteger)timeZoneOffset
{
    NSDate *date = [NSDate date];
    NSTimeZone *zone = [NSTimeZone systemTimeZone];
    NSInteger offset = [zone secondsFromGMTForDate:date];
    return offset;
}
/**
 *  根据给出的日期，返回该日期的前一天的起点（相对于日期起点，seconds的计数）
 *
 *  @param date 日期
 *
 *  @return 返回的是seconds计数，相对于日历系统起始至该日期前一天的起点（就是0点）
 */
+ (NSInteger)preDayOfTheDate_start:(NSDate *)date{
    NSCalendar *cal = [NSCalendar currentCalendar];
    NSDateComponents *components = [cal components:(NSHourCalendarUnit | NSMinuteCalendarUnit|NSSecondCalendarUnit) fromDate:date];
    [components setHour:-[components hour]-24];
    [components setMinute:-[components minute]];
    [components setSecond:-[components second]];
    NSDate *date1 = [cal dateByAddingComponents:components toDate:date options:0];
    
    return [date1 timeIntervalSince1970]+[self timeZoneOffset];
}
/**
 *  根据给出的日期，返回该日期的前一天的终点(也就是给出日期的起点）
 *
 *  @param date 给定的日期
 *
 *  @return 返回的是seconds计数
 */
+ (NSInteger)preDayOfTheDate_end:(NSDate *)date{
    NSCalendar *cal = [NSCalendar currentCalendar];
    NSDateComponents *components = [cal components:(NSHourCalendarUnit | NSMinuteCalendarUnit|NSSecondCalendarUnit) fromDate:date];
    [components setHour:-[components hour]];
    [components setMinute:-[components minute]];
    [components setSecond:-[components second]];
    NSDate *date1 = [cal dateByAddingComponents:components toDate:date options:0];
    
    return [date1 timeIntervalSince1970]+[self timeZoneOffset];
}
/**
 *  根据给出的日期，返回该日期的后一天的起点（相对于日期起点，seconds的计数）
 *
 *  @param date 日期
 *
 *  @return 返回的是seconds计数，相对于日历系统起始至该日期前一天的起点（就是0点）
 */
+ (NSInteger)nextDayOfTheDate_start:(NSDate *)date{
    NSCalendar *cal = [NSCalendar currentCalendar];
    NSDateComponents *components = [cal components:(NSHourCalendarUnit | NSMinuteCalendarUnit|NSSecondCalendarUnit) fromDate:date];
    [components setHour:-[components hour]+24];
    [components setMinute:-[components minute]];
    [components setSecond:-[components second]];
    NSDate *date1 = [cal dateByAddingComponents:components toDate:date options:0];
    
    return [date1 timeIntervalSince1970]+[self timeZoneOffset];
    
}
/**
 *  根据给出的日期，返回该日期的后一天的终点(也就是给出日期的起点）
 *
 *  @param date 给定的日期
 *
 *  @return 返回的是seconds计数
 */
+ (NSInteger)nextDayOfTheDate_end:(NSDate *)date{
    NSCalendar *cal = [NSCalendar currentCalendar];
    NSDateComponents *components = [cal components:(NSHourCalendarUnit | NSMinuteCalendarUnit|NSSecondCalendarUnit) fromDate:date];
    [components setHour:-[components hour]+ 2*24];
    [components setMinute:-[components minute]];
    [components setSecond:-[components second]];
    NSDate *date1 = [cal dateByAddingComponents:components toDate:date options:0];
    
    return [date1 timeIntervalSince1970]+[self timeZoneOffset];
    
}

@end
